import axios from './axios.ts'

// 发布文章
export function publishEssay(data:object):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'post',
      url: 'api/publishEssay',
      data
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 获取空间内所有文章
export function getSpaceEssay(spaceId:number):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'get',
      url: `api/getSpaceEssay?spaceId=${spaceId}`,
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 删除文章
export function deleteEssay(essayId:number):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'get',
      url: `api/deleteEssay?essayId=${essayId}`,
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 点赞文章
export function likeEssay(data:object):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'post',
      url: `api/likeEssay`,
      data
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 评论文章
export function commentEssay(data:object):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'post',
      url: `api/commentEssay`,
      data
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 收藏文章
export function collectEssay(data:object):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'post',
      url: `api/collectEssay`,
      data
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 根据用户id查询用户发布的所有文章
export function GetUserEssay(user_id:number):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'get',
      url: `api/getUserEssay?user_id=${user_id}`,
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}

// 根据用户id查询用户收藏的所有文章
export function GetUserCollectEssay(user_id:number):any{
  return new Promise((resolve, reject) => {
    axios({
      method:'get',
      url: `api/getUserCollectEssay?user_id=${user_id}`,
    }).then(res => {
      resolve(res.data)
    }).catch(() => {
      window.$message.error("接口异常")
      reject(new Error("接口异常"))
    })
  })
}
